import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluro/fluro.dart';

class NativeWidgetList extends StatelessWidget {
  NativeWidgetList({Key? key}) : super(key: key);
  final List<String> widgets = [
    'AboutDialog',
    'AboutListTile',
    'AbsorbPointer',
    'Chip',
    'AlertDialog',
    'SimpleDialog',
    'Layout',
  ];
  final List<String> routeNames = [
    '/license_index_page',
    '/license_tile_page',
    '/absorb_pointer_page',
    '/chip_page',
    '/',
    '/',
    '/layout_page1',
  ];
  cellClickAction(BuildContext context, String routerName, int index) {
    if (index == 4) {
      showDialogAction(context);
    } else if (index == 5) { 
      showSimpleDialogAction(context);
    } else {
      FluroRouter.appRouter.navigateTo(context, routerName, transition: TransitionType.native);
    }
  }
  // AlertDialog
  showDialogAction (BuildContext context) {
    showDialog(
      context: context, 
      builder: (BuildContext context) => AlertDialog(
        title: const Text('提示'),
        content: const Text('确认删除吗？'),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(), 
            child: const Text('取消'),
          ),
          TextButton(
            onPressed: () => FluroRouter.appRouter.pop(context), 
            child: const Text('确认'),
          ),
        ],
      ),
    );
  }
  // CupertinoAlertDialog
  Future<void> showSimpleDialogAction(BuildContext context) async {
    switch (
      await showDialog(
        context: context, 
        builder: (BuildContext context) => SimpleDialog(
          title: const Text('Select assignment'),
          children: [
            Container(
              height: 80,
              alignment: Alignment.center,
              child: const Text('确认删除吗？'),
            ),
            const Divider(height: 1),
            TextButton(
              onPressed: () => Navigator.pop(context, 'cancel'), 
              child: const Text('取消'),
            ),
            const Divider(height: 1),
            TextButton(
              onPressed: () => Navigator.of(context).pop('ok'), 
              child: const Text('确定'),
            ),
          ],
        ) ,
      )
    ) {
      case 'cancel':
        print('click cancel');
        break;
      case 'ok':
        print('click ok');
        break;
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("flutter widget list"),
      ),
      body: ListView.builder(
        itemBuilder: (context, index) {
          String name = widgets.elementAt(index);
          String routerName = routeNames.elementAt(index);
          return _buildRow(context, name, routerName, index);
        },
        itemCount: widgets.length,
      ),
    );
  }
  Widget _buildRow(BuildContext context, String name, String routerName, int index) {
    return Column(
      children: [
        ListTile(
          leading: CircleAvatar(
            child: Text(index.toString()),
          ),
          trailing: const Icon(Icons.arrow_forward_ios_sharp),
          title: Text(name),
          onTap: () => cellClickAction(context, routerName, index),
        ),
        const Divider(color: Color.fromRGBO(53, 53, 53, 0.5), height: 1),
      ],
    );
  }
}